//
//  YTSetUtils.m
//  YUNT
//
//  Created by 梁炜东 on 2017/11/20.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "YTSetUtils.h"

@interface YTSetUtils()

@property(nonatomic, copy)SetUtilsBlock block;

@end

@implementation YTSetUtils

-(instancetype)init
{
    self = [super init];
    if (self)
    {
        self.layer.cornerRadius = 6;
        self.layer.masksToBounds = YES;
        self.layer.borderWidth = 1;
        self.layer.borderColor = YTColorAlpha(210, 210, 210, 0.5).CGColor;
    }
    return self;
}
-(void)showWithTexts:(NSArray *)texts block:(SetUtilsBlock)block
{
    self.hidden = NO;
    self.block = block;
//    self.size = CGSizeMake(70*texts.count, 50);
    for (int i = 0; i < texts.count; i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat width = 70, height = 50, x = width*i, y = 0;
        btn.frame = CGRectMake(x, y, width, height);
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        [btn setTitle:texts[i] forState:UIControlStateNormal];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        btn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 600+i;
        [self addSubview:btn];
        if (i != 0)
        {
            UIView *line = [[UIView alloc] init];
            line.backgroundColor = YTColorAlpha(210, 210, 210, 0.5);
            line.frame = CGRectMake(0, 0, 1, height);
            [btn addSubview:line];
        }
    }
}
-(void)btnAction:(UIButton *)sender
{
    NSArray *subViews = [self subviews];
    for (id btn in subViews)
    {
        if ([btn class] == [UIButton class])
        {
            UIButton *button = (UIButton *)btn;
            if (button.tag == sender.tag)
            {
                button.selected = YES;
                button.backgroundColor = [UIColor whiteColor];
            }else
            {
                button.selected = NO;
                button.backgroundColor = [UIColor clearColor];
            }
        }
    }
    self.block(sender.tag - 600, sender);
}
@end
